package service.impl;

import dao.MiddleDao;
import dao.RoleDao;
import dao.impl.MiddleDaoImpl;
import dao.impl.RoleDaoImpl;
import model.Menu;
import model.Role;
import service.RoleService;

import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    RoleDao roleDao = new RoleDaoImpl();
    MiddleDao middleDao = new MiddleDaoImpl();

    @Override
    public List<Role> getRole() {
        return roleDao.getRole();
    }

    @Override
    public List<Role> getAllRole(int pageIndex, int pageSize) {
        return roleDao.getAllRole(pageIndex, pageSize);
    }

    @Override
    public int total() {
        return roleDao.total();
    }

    @Override
    public int insertRole(String rolename, String state, String[] ids) {
        //考虑事务问题
        int flag = 0;
        //1. 新增角色表
        try {
            Role role = new Role();
            role.setRoleName(rolename);
            role.setRoleState(Integer.parseInt(state));
            int k = roleDao.insert(role);//返回新增角色的id值
            //2. 新增中间表
            middleDao.insertMiddle(k, ids);
            flag = 1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Role getRoleById(int id) {
        Role roleById = roleDao.getRoleById(id);
        List<Menu> menus = roleById.getMenus();//未分级角色菜单
        List<Menu> newMenuList = new ArrayList<>();//保存分级以后的菜单
        for (Menu menu : menus) {
            if (menu.getUpmenuId() == 0) {
                //说明是一级菜单
                List<Menu> secondList = new ArrayList<>();
                for (Menu second : menus) {
                    if (second.getUpmenuId() == menu.getMenuId()) {
                        secondList.add(second);
                    }
                }
                menu.setSecondMenus(secondList);
                newMenuList.add(menu);
                //这时候已经分完级别
            }
        }
        roleById.setMenus(newMenuList);
        return roleById;
    }

    @Override
    public int update(int roleid, String rolename, String state, String[] ids) {
        //考虑事务问题
        int flag = 0;
        //1. 新增角色表
        try {
            Role role = new Role();
            role.setRoleId(roleid);
            role.setRoleName(rolename);
            role.setRoleState(Integer.parseInt(state));
            //先根据id删除中间表
            int i = middleDao.deleteByRoleId(roleid);
            if (i > 0) {
                //执行更新
                roleDao.update(role);//返回影响记录条数
                //2. 新增中间表
                middleDao.insertMiddle(roleid, ids);
                flag = 1;
            } else {
                flag = 0;
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int delete(int roleid) {
        int flag = 0;
        int i = middleDao.deleteByRoleId(roleid);
        if (i > 0) {
            flag = roleDao.delete(roleid);
        }
        return flag;
    }

}
